package com.lb.anno;

import java.lang.reflect.Field;

/**
 * @Author: zhangjun
 * @Date: 2020/5/21 10:38
 * @Description:
 */
public class AnnotationUtils {
    public static String getObject(Class<?> clazz) {
        String result = "";
        //获取字段
        Field[] fields = clazz.getDeclaredFields();

        //判断字段上的注解
        for (Field f: fields) {
            if (f.isAnnotationPresent(Name.class)) {
                //获取注解上的信息
                Name name = f.getAnnotation(Name.class);
                result += f.getName() + ":" + name.value() + "\n";
            }

            if (f.isAnnotationPresent(Sex.class)) {
                //获取注解上的信息
                Sex sex = f.getAnnotation(Sex.class);
                result += f.getName() + ":" + sex.gender() + "\n";
            }
        }
        return result;
    }
    public static void main(String[] args) {
        String res = AnnotationUtils.getObject(Person.class);
        System.out.println(res);
    }
}
